<script lang="ts" setup name="XtxConfirm">
import XtxButton from '../button/index.vue'
import { onMounted, PropType, ref } from 'vue'

defineProps({
  title: {
    type: String,
    required: true,
  },
  text: {
    type: String,
    required: true,
  },
  confirmCallback: {
    type: Function as PropType<() => void>,
  },
  cancelCallback: {
    type: Function as PropType<() => void>,
  },
})

const isShow = ref(false)
onMounted(() => {
  // onMounted 仅仅是dom准备好了, 你能拿到了, 但是不代表渲染出来了
  // 这个延迟时间, 可以短一些, 但是不能删, 因为要等初始状态完完全全被页面渲染出来, 才行

  // 要先等初始状态, 必须浏览器渲染完了, 才能修改到最终状态
  setTimeout(() => {
    isShow.value = true
  }, 20)
})
</script>

<template><!-- 
    1. 这个盒子从你一点击后, 其实就会显示
    2. 可以让他的初始状态为  透明状态
    3. dom一渲染解析完(透明的盒子刚出来), 立刻修改状态, 将他改成最终的样式 
  -->
    <div class="xtx-confirm" :class="{ fadeIn: isShow }">
      <div class="wrapper" :class="{ fadeIn: isShow }">
        <div class="header">
          <h3>{{ title }}</h3>
          <a @click="cancelCallback" href="JavaScript:;" class="iconfont icon-close-new"></a>
        </div>
        <div class="body">
          <i class="iconfont icon-warning"></i>
          <span>{{ text }}</span>
        </div>
        <div class="footer">
          <!-- 到底确认和取消, 点击时做什么事情, 不是组件内写死的 -->
          <XtxButton size="mini" type="gray" @click="cancelCallback">
            取消
          </XtxButton>
          <XtxButton size="mini" type="primary" @click="confirmCallback">
            确认
          </XtxButton>
        </div>
      </div>
    </div>
</template>

<style scoped lang="less">
.xtx-confirm {
  // 默认撑满整个屏幕的容器
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 8888;
  background: rgba(0, 0, 0, 0); // 纯黑全透明

  &.fadeIn {
    transition: all 0.4s;
    background: rgba(0, 0, 0, 0.5); // 纯黑半透明
  }

  .wrapper {
    width: 400px;
    background: #fff;
    border-radius: 4px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -65%);
    opacity: 0;

    &.fadeIn {
      transition: all 0.4s;
      transform: translate(-50%, -50%);
      opacity: 1;
    }

    .header,
    .footer {
      height: 50px;
      line-height: 50px;
      padding: 0 20px;
    }

    .body {
      padding: 20px 40px;
      font-size: 16px;

      .icon-warning {
        color: @priceColor;
        margin-right: 3px;
        font-size: 16px;
      }
    }

    .footer {
      text-align: right;

      .xtx-button {
        margin-left: 20px;
      }
    }

    .header {
      position: relative;

      h3 {
        font-weight: normal;
        font-size: 18px;
      }

      a {
        position: absolute;
        right: 15px;
        top: 15px;
        font-size: 20px;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        color: #999;

        &:hover {
          color: #666;
        }
      }
    }
  }
}

.confirm-enter-from {
  opacity: 0;
}

.confirm-enter-active {
  transition: all 0.5s;
}

.confirm-enter-to {
  opacity: 1;
}</style>
